#!/usr/bin/env python
##================
##gui example
##================
import pygame,sys
import lwpgt
SW, SH=640, 480
#main screen variable
SCREEN=pygame.display.set_mode((SW, SH))
#init pygame
pygame.init()
pygame.key.set_repeat(250, 50)
pygame.display.set_caption("2D Render Exmaple")

class render2dexample01:
    """render2d example"""
    def __init__(self):
        """init example"""
        #loading tiles
        self.DB=lwpgt.tools.ZipDB("data/tiles.zip")
        self.tile0=lwpgt.render2d.Tile(self.DB.GetImage("tile.bmp"))
        self.tile1=lwpgt.render2d.Tile(self.DB.GetImage("tile1.bmp"))
        self.herotile=lwpgt.render2d.Tile(self.DB.GetImage("hero.bmp"))
        #create camera and map
        self.camera=lwpgt.render2d.Camera(pygame.Rect(0, 0, 320, 240))
        self.mapka=lwpgt.render2d.Maps()
        #setting up hero
        self.hero=lwpgt.render2d.Entity(self.herotile)
        self.hero.SetPosition(0, 0)
        self.herotile.SetAnimationFrames([0, 1])
        self.hero.tile.SetAnimationDelay(0.100)
        
        #tiles list
        self.tiles=[self.tile0, self.tile1]
        #load map from file
        self.mapka.LoadMapFromXML("testmap.xml", self.tiles)
        
    def update(self):
        """update screen"""
        pass

    def redraw(self,screen):
        """redraw screen"""
        screen.fill((0,0,64))
        ##drawing here
        self.mapka.Render(SCREEN, self.camera)
        self.hero.Draw(SCREEN, self.camera)
        pygame.display.flip()
        
    
    def events(self):
        """events handler for example"""
        for e in pygame.event.get():
            if e.type==pygame.QUIT:
                sys.exit()
            if e.type==pygame.KEYDOWN:
                if e.key==pygame.K_LEFT:
                    self.herotile.SetAnimationFrames([2, 3])
                    self.hero.MoveObject(-16, 0)
                if e.key==pygame.K_RIGHT:
                    self.herotile.SetAnimationFrames([4, 5])
                    self.hero.MoveObject(16, 0)
                if e.key==pygame.K_UP:self.hero.MoveObject(0, -16)
                if e.key==pygame.K_DOWN:self.hero.MoveObject(0, 16)
            else:
                self.herotile.SetAnimationFrames([0, 1])
        self.camera.CenterAt(self.hero.GetScreenPosition())

    def mainloop(self):
        """main loop """
        while 1:
            self.events()
            self.update()
            self.redraw(SCREEN)
    
if __name__=="__main__":
    render2dexample01().mainloop()
